//
//  LKMytradeCountSonLeftViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/22.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKMytradeCountSonLeftViewController.h"
#import "LKMyShopDetailDateView.h"
#import "GFDatePicker.h"
#import "LKMyShopDetailPriceCell.h"
#import "LKMyTradeCountModel.h"
#import "LKMyShopDetailTableHeaderView.h"
#import "LKTradeWebViewCell.h"
#import "LKPickeCustomView.h"
@interface LKMytradeCountSonLeftViewController ()<UITableViewDelegate,UITableViewDataSource,GetContentInformation>
@property(nonatomic,strong)LKMyShopDetailDateView *dateView;
@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)LKMyTradeCountModel *model;
@property(nonatomic,strong)NSMutableArray *dateArray;
@property(nonatomic,strong)LKPickeCustomView *sorcepickerView;
@end

@implementation LKMytradeCountSonLeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dateArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 6; i++) {
        NSString *dateStr = [NSString getTimeStrAccoringDate:[self getPriousorLaterDateFromDate:[NSDate date] withMonth:-i]];
        [self.dateArray addObject:dateStr];
    }
    NSLog(@"上个月%@",self.dateArray);
    [self getMyDataInformationQWithType:@"1"];
    self.dateView = [[[NSBundle mainBundle] loadNibNamed:@"LKMyShopDetailDateView" owner:self options:nil] lastObject];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(datePickerViewShow:)];
    [self.dateView.dateView addGestureRecognizer:tap];
    NSString *currentStr = [NSString getCurrentTime];
    NSString *currentTime = [currentStr componentsSeparatedByString:@" "][0];
    self.dateView.monthLabel.text = [currentTime componentsSeparatedByString:@"-"][1];
    self.dateView.yearLabel.text = [currentTime componentsSeparatedByString:@"-"][0];
    [self.view addSubview:_dateView];
    [_dateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(0);
        make.height.mas_equalTo(62);
    }];
    [self createMyTableView];
    // Do any additional setup after loading the view.
}
-(void)createMyTableView{
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKMyShopDetailPriceCell",@"LKTradeWebViewCell"]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(_dateView.mas_bottom).offset(10);
        make.bottom.equalTo(self.view).offset(-10);
    }];
    [self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return self.model.trade_amount.count;
    }
    if (section == 3) {
        return self.model.trade_number.count;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        LKTradeWebViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKTradeWebViewCell"];
        cell.header = @"交易金额分布";
        cell.url = self.model.trade_amount_excel;
        return cell;
    }
    if (indexPath.section == 1) {
        LKMyShopDetailPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKMyShopDetailPriceCell"];
        cell.tradeCountSonModel = self.model.trade_amount[indexPath.row];
        return cell;
    }
    if (indexPath.section == 3) {
        LKMyShopDetailPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKMyShopDetailPriceCell"];
        cell.tradeCountSonModel = self.model.trade_number[indexPath.row];
        return cell;
    }
    if (indexPath.section == 2) {
        LKTradeWebViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKTradeWebViewCell"];
        cell.header = @"交易笔数分布";
        cell.url = self.model.trade_number_excel;
        return cell;
    }
    LKMyShopDetailPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKMyShopDetailPriceCell"];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 || indexPath.section == 2) {
        return 250.0f;
    }
    return 52.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LKMyShopDetailTableHeaderView *header = [[[NSBundle mainBundle] loadNibNamed:@"LKMyShopDetailTableHeaderView" owner:self options:nil] lastObject];
    header.backgroundColor = COLOR_HEX(0xFDF8F5);
    if (section == 1) {
        header.isTradeCountFirst = YES;
    }
    if (section == 3) {
        header.isTradeCountSecond = YES;
    }
    return header;

}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1 || section == 3) {
        return 52.f;
    }
    return 0.000000001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1.0f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

/// 时间选择器
/// @param tap 点击事件
-(void)datePickerViewShow:(UITapGestureRecognizer *)tap{
    WS(weakSelf)
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    if (!_sorcepickerView) {
        _sorcepickerView = [[LKPickeCustomView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _sorcepickerView.font = [UIFont systemFontOfSize:20];
        _sorcepickerView.delegate = weakSelf;
        _sorcepickerView.contentArray = weakSelf.dateArray;
        _sorcepickerView.selectValue = _sorcepickerView.contentArray[0];
        [_sorcepickerView.pickView reloadAllComponents];
    }
    [[[UIApplication sharedApplication] keyWindow] addSubview:self.sorcepickerView];
    
}

/// 获取到的值
/// @param sex 获取到的值
/// @param pickeView pickerview
-(void)getContentInformation:(NSString *)sex AndPickerView:(UIPickerView *)pickeView{
    [self.sorcepickerView removeFromSuperview];
    NSLog(@"选中%@",sex);
    NSLog(@"选中的数=%ld",self.sorcepickerView.selectRow);
    self.dateView.monthLabel.text = [sex componentsSeparatedByString:@"-"][1];
    self.dateView.yearLabel.text = [sex componentsSeparatedByString:@"-"][0];
    [self getMyDataInformationQWithType:[NSString stringWithFormat:@"%ld",self.sorcepickerView.selectRow + 1]];
}
/**
 取消的代理放啊
 */
-(void)ContentPickerCancleActionAndPickerView:(UIPickerView *)pickeView{
    [self.sorcepickerView removeFromSuperview];
}
/// 获取报表数据
-(void)getMyDataInformationQWithType:(NSString *)type{
    
    NSDictionary *param = @{@"type":type};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyTradeCountRatioUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            self.model = [LKMyTradeCountModel modelWithDictionary:responseObject[@"data"]];
            self.dateView.tradeCountModel = self.model;
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}

/// 获取当前日期的前一部分时间
/// @param date 当前日期
/// @param month 月份
- (NSDate*)getPriousorLaterDateFromDate:(NSDate*)date withMonth:(int)month{

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];// NSGregorianCalendar
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    return mDate;

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
